import { defineStore } from 'pinia'
import { getToken, setToken,removeToken } from '../utils/token-utils';
import {ElMessage} from 'element-plus'
import loginApi from '@/api/login'
export const useUserInfoStore = defineStore('login', {
    state:()=>({
        token: getToken() as string,
        username: 'admin',
        password: '111111',
        MemberList:[]
    }),
    actions:{
        login (username: string, password: string) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    if (username==='admin' && password==='111111') {
                        const token = '5858c60a-7a7c-4593-869e-3019e126c4f2'
                        setToken(token)
                        this.token = token
                        resolve(token)
                    } else {
                        reject(new Error('用户名或密码错误!'))
                        ElMessage.error('用户名或密码错误!')
                    }
                }, 1000)
            })
        },
        // 退出登录
        reset () {
            // 删除local中保存的token
            removeToken()
            // 提交重置用户信息的mutation
            this.token =''
            ElMessage({
                type: 'success',
                message: '退出登录成功'
            })
        },
        //获取用户信息
        async getList(){
            try{
                let result = await  loginApi.getUserInfo();
                // console.log(result)
            }catch (error) {
                console.error(error);
            }
        },
    }
})
